﻿using System;
using System.Data;
using System.Web.UI;
using mojoPortal.Web.Framework;
using Resources;
using ud_Article.Business;

namespace ud_Article.UI
{
    public partial class StatsControl : UserControl
    {
        private int pageId = -1;
        private int moduleId = -1;
        private int countOfDrafts;
        private bool showCommentCount = true;

        public int PageId
        {
            get { return pageId; }
            set { pageId = value; }
        }

        public int ModuleId
        {
            get { return moduleId; }
            set { moduleId = value; }
        }

        public int CountOfDrafts
        {
            get { return countOfDrafts; }
            set { countOfDrafts = value; }
        }

        public bool ShowCommentCount
        {
            get { return showCommentCount; }
            set { showCommentCount = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnPreRender(EventArgs e)
        {
            if (Visible)
            {
                PopulateControls();
            }
            base.OnPreRender(e);
        }

        private void PopulateControls()
        {
            if (pageId == -1) { return; }
            if (moduleId == -1) { return; }

            using (IDataReader reader = Article.GetBlogStats(ModuleId))
            {
                if (reader.Read())
                {
                    int entryCount = Convert.ToInt32(reader["EntryCount"]);
                    int commentCount = Convert.ToInt32(reader["CommentCount"]);
                    litEntryCount.Text = ResourceHelper.FormatCategoryLinkText(ArticleResources.BlogEntryCountLabel, (entryCount - countOfDrafts));
                    litCommentCount.Text = ResourceHelper.FormatCategoryLinkText(ArticleResources.BlogCommentCountLabel, commentCount);

                }
                else
                {
                    litEntryCount.Text = ResourceHelper.FormatCategoryLinkText(ArticleResources.BlogEntryCountLabel, 0);
                    litCommentCount.Text = ResourceHelper.FormatCategoryLinkText(ArticleResources.BlogCommentCountLabel, 0);
                }

            }

            liComments.Visible = showCommentCount;

        }
    }
}